package edu.uta.BillMeApp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import edu.BillME.datapars.HttpConnection;
import edu.uta.BillMeApp.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddFriend extends Activity {
	 EditText firstname,lastname,email;
     TextView result;
     Button add;
     String q;
     /* @auther Siddharth Patel & Susan Foster */
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addfriend);
        
        lastname=(EditText)findViewById(R.id.lastname);
        email=(EditText)findViewById(R.id.email);
        add=(Button)findViewById(R.id.addfrnd);
        	result=(TextView)findViewById(R.id.result);
        	 Bundle gotBundle=getIntent().getExtras();
             final String getUN = gotBundle.getString("wN");
        	
        	add.setOnClickListener(new View.OnClickListener() {
        		//@Override
        		public void onClick(View v) {
        			// TODO Auto-generated method stub
        			q="friend";
        			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        			postParameters.add(new BasicNameValuePair("firstname", getUN));
        			postParameters.add(new BasicNameValuePair("lastname", lastname.getText().toString()));
        			postParameters.add(new BasicNameValuePair("email", email.getText().toString()));
        			String response = null;
        			try {
						response = HttpConnection.executeHttpPost("http://omega.uta.edu/~smp3753/friend.php", postParameters);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
        			String res=response.toString();
        			res=res.replace("\n", "");
        			Log.d("res",res);
        			if(res.contains("1")){
        				result.setText("Friend added");
        				
        				Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        				String[] recipients = new String[]{email.toString(), "",};
        				emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
        				emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Billme Reminder");
        				emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"You have been added as a friend on the BillMe application by "+getUN);
        				emailIntent.setType("text/plain");
        				startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        				finish();
        				}
        				else{
        				result.setText("Please try again");
        					
        				}
        	}

        
    });
    }
}